<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
       <el-form-item label="员工姓名" prop="realName">
        <el-input
          v-model="queryParams.realName"
          placeholder="请输入员工姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="手机号" prop="phone">
        <el-input
          v-model="queryParams.phone"
          placeholder="请输入手机号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="公司名称" prop="company">
        <el-select clearable v-model="queryParams.company" filterable placeholder="请选择">
          <el-option
            v-for="item in companyOptions"
            :key="item.value"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="职级职等" prop="post">
        <el-select clearable v-model="queryParams.post" filterable placeholder="请选择">
          <el-option
            v-for="item in postOptions"
            :key="item.value"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
       <el-form-item label="员工状态" prop="employeeStatus">
        <el-select clearable
            class="toolbar-lable-right"
            v-model="queryParams.employeeStatus"
            placeholder="请选择" size="small">
            <el-option
              v-for="item in empStatus"
              :label="item.name"
              :value="item.value">
            </el-option>
          </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="userList" @selection-change="handleSelectionChange">
       <el-table-column type="index" label="序号" width="80" />
      <el-table-column label="员工姓名" align="center" prop="realName" />
      <el-table-column label="性别" align="center" prop="sex" />

      <el-table-column label="手机号" align="center" prop="phone" />
      <el-table-column label="职级职等" align="center" prop="post" />
      <el-table-column label="所在公司" align="center" prop="company" />
      <el-table-column label="部门" align="center" prop="departmentName" />
      <el-table-column label="生日" align="center" prop="birthday" width="180">
      </el-table-column>
      <el-table-column label="入职时间" align="center" prop="joinDate" width="180">
      </el-table-column>
      <el-table-column label="家庭地址" align="center" prop="address" />
      <el-table-column label="员工状态" align="center" prop="employeeStatus">
      <template slot-scope="scope">
            {{ codeBox.EMPLOYEE_STATUS[scope.row.employeeStatus] }}
          </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
          >编辑</el-button>
          <!-- <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
          >删除</el-button> -->
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.page"
      :limit.sync="queryParams.limit"
      @pagination="getList"
    />

    <!-- 添加或修改员工管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item hidden label="主键" align="center" prop="id" />
        <el-form-item label="职级职等" prop="post">
          <el-select clearable v-model="form.post" filterable placeholder="请选择">
          <el-option
            v-for="item in postOptions"
            :key="item.value"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { queryEmployeeList, queryCompanyList, queryPostList, updateEmployee } from "@/api/system"
import Pagination from "@/components/Pagination";
import { getCodeManager,codeToArray, getSellers} from '@/api/all'

export default {
  name: "User",
  components: { Pagination },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      empStatus:[],
      companyOptions: [],
      postOptions: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 员工管理表格数据
      userList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        page: 1,
        limit: 10,
        phone: null,
        realName: null,
        company: null,
        post: null,
        employeeStatus: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    }
  },
  created() {
    getCodeManager('EMPLOYEE_STATUS', () => {
      this.empStatus = codeToArray('EMPLOYEE_STATUS');
    })
    this.getList()
    this.getCompanyList(),
    this.getPostList()
  },
  methods: {
    getCompanyList() { 
      queryCompanyList().then(response => { 
        console.log(response.data)
        this.companyOptions = response.data.data.map(item => ({
          value: item,  // 选项的值
          label: item   // 选项的显示文本
        }));
        console.log("gongsi", this.companyOptions)
      })
    },
    getPostList() { 
      queryPostList().then(response => { 
        this.postOptions = response.data.data.map(item => ({
          value: item,  // 选项的值
          label: item   // 选项的显示文本
        }));
      })
    },
    /** 查询员工管理列表 */
    getList() {
      this.loading = true
      queryEmployeeList(this.queryParams).then(response => {
        this.userList = response.data.rows
        this.total = response.data.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        nickName: null,
        phone: null,
        account: null,
        realName: null,
        idCard: null,
        sex: null,
        email: null,
        departmentCode: null,
        departmentName: null,
        isStatus: null,
        delFlag: null,
        company: null,
        birthday: null,
        joinDate: null,
        address: null,
        employeeStatus: null
      }
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.page = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams.realName = null
      this.queryParams.phone = null
      this.queryParams.company = null
      this.queryParams.post = null
      this.queryParams.employeeStatus = null
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = "添加员工管理"
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const id = row.id || this.ids
      this.form = row
      this.open = true
      this.title = "编辑员工管理"
    },
    /** 提交按钮 */
    submitForm() {
      console.log("form", this.form)
      this.$refs["form"].validate(valid => {
        if (valid) {
            updateEmployee(this.form).then(response => {
              this.$modal.msgSuccess("修改成功")
              this.open = false
              this.getList()
            })
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$modal.confirm('是否确认删除员工管理编号为"' + ids + '"的数据项？').then(function() {
        return delUser(ids)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess("删除成功")
      }).catch(() => {})
    },
  }
}
</script>
